package com.example.demo.controller;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.service.SpringbootAyscService;
import com.example.demo.service.impl.RunableTask;
import com.sun.org.slf4j.internal.LoggerFactory;

/**
 * springboot 异步执行  在主线程之外开启另外的线程
 * @author jiu'yue
 *
 */
@Controller
@RequestMapping("aysc")
@ResponseBody
@EnableAsync
public class SpringbootAyscController {
	
	@Resource
	private SpringbootAyscService springbootAyscService;
	
	
	/**
     * 异步处理1：使用springBoot自带async注解
     */
    @RequestMapping(value = "testAysc",method = RequestMethod.GET)
    public String testAysc(){
    	springbootAyscService.testAysc();
        System.out.println("============>"+Thread.currentThread().getName());
        return "异步,正在解析......";
    }
    
    /**
     * 异步处理2  线程池实现
     */
    @RequestMapping(value = "testAyscThread",method = RequestMethod.GET)
    public String testAyscThread(){
    	ExecutorService service = Executors.newFixedThreadPool(5);
    	RunableTask task = new RunableTask();
    	service.execute(task);
        System.out.println("============>"+Thread.currentThread().getName());
        return "异步,正在解析......";
    }
    
    
}
